package com.ruoyi.control.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnowflakeIdUtils;
import com.ruoyi.control.domain.MonorailcraneHistory;
import com.ruoyi.control.mapper.MonorailcraneHistoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.MonorailcraneMapper;
import com.ruoyi.control.domain.Monorailcrane;
import com.ruoyi.control.service.IMonorailcraneService;

/**
 * 单轨吊Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-20
 */
@Service
public class MonorailcraneServiceImpl implements IMonorailcraneService
{
    @Autowired
    private MonorailcraneMapper monorailcraneMapper;
    @Autowired
    private MonorailcraneHistoryMapper monorailcraneHistoryMapper;

    /**
     * 查询单轨吊
     *
     * @param tagName 单轨吊主键
     * @return 单轨吊
     */
    @Override
    public Monorailcrane selectMonorailcraneByTagName(String tagName)
    {
        return monorailcraneMapper.selectMonorailcraneByTagName(tagName);
    }

    /**
     * 查询单轨吊列表
     *
     * @param monorailcrane 单轨吊
     * @return 单轨吊
     */
    @Override
    public List<Monorailcrane> selectMonorailcraneList(Monorailcrane monorailcrane)
    {
        Monorailcrane monorailcrane2= new Monorailcrane();
        BeanUtils.copyProperties(monorailcrane,monorailcrane2);
        if ("5555555555555555555".equals(monorailcrane.getRespectDataType())){
            monorailcrane2.setRespectDataType(null);
            String[] arr={"网络连接","运行速度","运行状态","给定频率",
                    "位置","从站数据","刹车输入","刹车输出","速度输入","速度输出","勾数"};
            if (monorailcraneMapper.selectMonorailcraneByTagName("网络连接") != null){
                for (int i=0;i< arr.length ;i++){
                    Monorailcrane monorailcrane1=new Monorailcrane();
                    MonorailcraneHistory monorailcraneHistory=new MonorailcraneHistory();
                    monorailcrane1.setTagName(arr[i]);
                    switch (arr[i]){
                        case "网络连接":
                            monorailcrane1.setValue("正常");
                            break;
                        case "运行速度":
                            monorailcrane1.setValue("0.00m/s");
                            break;
                        case "运行状态":
                            monorailcrane1.setValue("等待使用");
                            break;
                        case "给定频率":
                            monorailcrane1.setValue("0.00HZ");
                            break;
                        case "位置":
                            monorailcrane1.setValue("8.42米");
                            break;
                        case "从站数据":
                            monorailcrane1.setValue("0.00脉冲");
                            break;
                        case "刹车输入":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "刹车输出":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "速度输入":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "速度输出":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "勾数":
                            monorailcrane1.setValue("23次");
                            break;
                    }
                    monorailcraneMapper.updateMonorailcrane(monorailcrane1);
                    monorailcrane1.setAddress(arr[i]);
                    monorailcrane1.setDescription(arr[i]);
                    monorailcrane1.setUpdateTime(new Date());
                    monorailcrane1.setDataType("String");
                    monorailcrane1.setClientAccess("RO");
                    monorailcrane1.setRespectDataType("1");
                    monorailcrane1.setScanRate("100");
                    BeanUtils.copyProperties(monorailcrane1,monorailcraneHistory);
                    monorailcraneHistory.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                    monorailcraneHistoryMapper.insertMonorailcraneHistory(monorailcraneHistory);
                }
            }else {
                for (int i=0;i< arr.length ;i++){
                    Monorailcrane monorailcrane1=new Monorailcrane();
                    MonorailcraneHistory monorailcraneHistory=new MonorailcraneHistory();
                    switch (arr[i]){
                        case "网络连接":
                            monorailcrane1.setValue("正常");
                            break;
                        case "运行速度":
                            monorailcrane1.setValue("0.00m/s");
                            break;
                        case "运行状态":
                            monorailcrane1.setValue("等待使用");
                            break;
                        case "给定频率":
                            monorailcrane1.setValue("0.00HZ");
                            break;
                        case "位置":
                            monorailcrane1.setValue("8.42米");
                            break;
                        case "从站数据":
                            monorailcrane1.setValue("0.00脉冲");
                            break;
                        case "刹车输入":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "刹车输出":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "速度输入":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "速度输出":
                            monorailcrane1.setValue("0.0DC/V");
                            break;
                        case "勾数":
                            monorailcrane1.setValue("23次");
                            break;
                    }
                    monorailcrane1.setTagName(arr[i]);
                    monorailcrane1.setAddress(arr[i]);
                    monorailcrane1.setDescription(arr[i]);
                    monorailcrane1.setUpdateTime(new Date());
                    monorailcrane1.setDataType("String");
                    monorailcrane1.setClientAccess("RO");
                    monorailcrane1.setRespectDataType("1");
                    monorailcrane1.setScanRate("100");
                    BeanUtils.copyProperties(monorailcrane1,monorailcraneHistory);
                    monorailcraneHistory.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                    monorailcraneHistoryMapper.insertMonorailcraneHistory(monorailcraneHistory);
                    monorailcraneMapper.insertMonorailcrane(monorailcrane1);
                }
            }
        }
        return monorailcraneMapper.selectMonorailcraneList(monorailcrane2);
    }

    /**
     * 新增单轨吊
     *
     * @param monorailcrane 单轨吊
     * @return 结果
     */
    @Override
    public int insertMonorailcrane(Monorailcrane monorailcrane)
    {
        return monorailcraneMapper.insertMonorailcrane(monorailcrane);
    }

    /**
     * 修改单轨吊
     *
     * @param monorailcrane 单轨吊
     * @return 结果
     */
    @Override
    public int updateMonorailcrane(Monorailcrane monorailcrane)
    {
        monorailcrane.setUpdateTime(DateUtils.getNowDate());
        return monorailcraneMapper.updateMonorailcrane(monorailcrane);
    }

    /**
     * 批量删除单轨吊
     *
     * @param tagNames 需要删除的单轨吊主键
     * @return 结果
     */
    @Override
    public int deleteMonorailcraneByTagNames(String[] tagNames)
    {
        return monorailcraneMapper.deleteMonorailcraneByTagNames(tagNames);
    }

    /**
     * 删除单轨吊信息
     *
     * @param tagName 单轨吊主键
     * @return 结果
     */
    @Override
    public int deleteMonorailcraneByTagName(String tagName)
    {
        return monorailcraneMapper.deleteMonorailcraneByTagName(tagName);
    }
}
